<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card class="box-card">
      <!--搜索区域-->
      <el-row :gutter="18">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryparams.query"  clearable @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"  @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!--用户列表区域-->
      <el-table
        :data="goodList"
        border
        style="width: 100%" :stripe="true">
        <el-table-column   type="index"></el-table-column>
        <el-table-column  prop="goods_name"  label="商品名称"   width="520"></el-table-column>
        <el-table-column  prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column  prop="goods_weight"    label="商品重量"></el-table-column>
        <el-table-column   label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column  label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit"   @click="handleEdit(scope.row.goods_id)"></el-button>
            <el-button type="danger" icon="el-icon-delete"   @click="handleDel(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryparams.pagenum"
        :page-sizes="[12, 24 , 48, 96]"
        :page-size="queryparams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'List',
    data:function () {
        return {
            goodList:[],
            queryparams:{
            query:'',
            pagenum: 1,
            pagesize:12,
          },
          total:0,
          addGoodDialogVisible:false
        }
    },
    created () {
      this.getGoodList();
    },
    methods:{
      async getGoodList(){
       var data=await this.$axios.get("goods",{params : this.queryparams});
       var res=data.data;
       if (res.meta.status!=200){ return  this.$message.error("获取商品数据失败!")}
       this.goodList=res.data.goods;
       this.total=res.data.total;
        console.log(res.data)
      },
      handleEdit(id){},
      handleDel(id){
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //处理删除
          var data=await this.$axios.delete('goods/'+id);
          var res=data.data;
          if(res.meta.status!=200){return this.$message.error("商品删除失败!")}
          //重新获取数据
          this.getGoodList();
          //提示删除成功
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(size){
        this.queryparams.pagesize=size;
        this.getGoodList();},
      handleCurrentChange(num){
        this.queryparams.pagenum=num;
        this.getGoodList();
      },
      goAddpage(){
        this.$router.push('/goods/add');
      }
    }
  }
</script>

<style scoped lang="less">
.el-table{
  margin-top: 10px;
}
</style>
